<?php

if (!defined('IN_XLP')) {
        exit('Access Denied!');
}

/**
 * Description of guessAction
 *
 * @author Administrator
 */
class guessAction extends commonAction {

        function __construct() {
                parent::__construct();
                parent::_checkLogin();
                $this->assign(array(
                  'status' => array(1 => '上线', 2 => '<span class="red">下线</span>'),
                  'difficulty' => array(1 => '困难', 2 => '容易'),
                  'source' => array(1 => '内部员工', 2 => '外部用户'),
                  'verify' => array(1 => '通过', 2 => '<span class="red" >不通过</span>', 3 => '<span class="red" >未审核</span>'),
                  'options' => array('A', 'B', 'C', 'D', 'E', 'F'),
                ));
        }

        public function index() {
                $p = $this->_getid('p', 1);
                $q = $this->_get('q', '');
                $diff = $this->_get('diff', 0);
                $sour = $this->_get('sour', 0);
                $sta = $this->_get('sta', 0);
                $ver = $this->_get('ver', 0);
                $where = array();
                if ($diff && ($diff <> 'all')) {
                        $where['difficulty'] = $diff;
                }
                if ($sour && ($sour <> 'all')) {
                        $where['source'] = $sour;
                }
                if ($sta && ($sta <> 'all')) {
                        $where['status'] = $sta;
                }
                if ($ver && ($ver <> 'all')) {
                        $where['verify'] = $ver;
                }
                if ($q) {
                        $where = 'instr(title,"' . M('guess_map')->getSafeStr($q) . '")>0';
                }
                $rs = M('guess_map')->where($where)->page($p)->order('infotime desc')->findAll();
                $this->assign(array(
                  'rs' => $rs,
                  'p' => $p,
                  'q' => $q,
                  'total' => M('guess_map')->getTotal(),
                  'diff' => $diff,
                  'sour' => $sour,
                  'sta' => $sta,
                  'ver' => $ver,
                  'verifyNo' => M('guess_map')->where(array('verify' => 3))->count('id'), //未审核题数
                  'statusNo' => M('guess_map')->where(array('status' => 2))->count('id'), //下线题数
                ));
                $this->display();
        }

        public function edit() {
                $id = $this->_getid('id', '');
                if (!$id) {
                        showError('参数错误');
                }
                $rs = M('guess_map')->where(array('id' => $id))->find();
                $this->assign(array('rs' => $rs));
                $this->display();
        }

        public function add() {
                $this->display();
        }

        public function post() {
                $credit_add = $this->_post('credit_add', '');
                $source = $this->_post('source', 1);
                $objData = array(
                  'title' => $this->_post('title', ''),
                  'pic' => $this->_post('pic', ''),
                  'content' => $this->_post('content', ''),
                  'options' => $this->_post('options', ''),
                  'credit_add' => $credit_add,
                  'credit_del' => $this->_post('credit_del', ''),
                  'difficulty' => $credit_add >= 4 ? 1 : 2,
                  'infotime' => TIME,
                  'user_id' => '',
                  'source' => $source,
                  'verify' => $source == 1 ? 1 : 3,
                  'status' => 1,
                  'question' => 0,
                );
                M('guess_map')->insert($objData);
                $this->JsonReturn('ok', null, 1);
        }

        /**
         * 栏目设置信息保存
         */
        public function setpost() {
                $this->setobjData();
                $this->JsonReturn('ok', null, 1);
        }

        public function save() {
                $pic = $this->_post('pic', '');
                $id = $this->_postid('id', '');
                $credit_add = $this->_post('credit_add', '');
                if (!$pic) {
                        $objData = array(
                          'title' => $this->_post('title', ''),
                          'content' => $this->_post('content', ''),
                          'options' => $this->_post('options', ''),
                          'credit_add' => $credit_add,
                          'credit_del' => $this->_post('credit_del', ''),
                          'difficulty' => $credit_add >= 4 ? 1 : 2,
                          'infotime' => TIME,
                          'source' => $this->_post('source', 1),
                        );
                } else {
                        $objData = array(
                          'title' => $this->_post('title', ''),
                          'pic' => $pic,
                          'content' => $this->_post('content', ''),
                          'options' => $this->_post('options', ''),
                          'credit_add' => $credit_add,
                          'credit_del' => $this->_post('credit_del', ''),
                          'difficulty' => $credit_add >= 4 ? 1 : 2,
                          'infotime' => TIME,
                          'source' => $this->_post('source', 1),
                        );
                }
                D('guess_map')->update($objData, array('id' => $id));
                $this->JsonReturn('ok', null, 1);
        }

        /**
         * 查看题目详情
         */
        public function getinfo() {
                $id = $this->_getid('id', '');
                if (!$id) {
                        showError('参数错误');
                }
                $this->assign(array('rs' => M('guess_map')->where(array('id' => $id))->find()));
                $this->display('info');
        }

        /**
         * 栏目设置
         */
        public function set() {
                $guess = F('guess/guess');
                if (!$guess) {
                        $guess = $this->setobjData(false);
                }
                $this->assign(array('rs' => $guess, 'tpls' => $this->getPluginTpl($guess['type'])));
                $this->display();
        }

        /**
         * 获取用户信息
         */
        public function userinfo() {
                $uid = $this->_getid('id', '');
                if (!$uid) {
                        showError('参数获取失败');
                }
                $rs = D('guess')->getuserinfo($uid);
                $this->assign(array('rs' => $rs));
                $this->display();
                //   print_r($rs);
        }

        /**
         * 删除已上传的图片
         */
        public function imgdelete() {
                $path = $this->_post('path', '');
                if (!$path) {
                        showError('参数错误');
                }
                $upolad = C('upload', 'dir');
                @unlink(ROOT . $upolad . str_replace('yun/', '/', $path));
                $this->JsonReturn('ok', null, 1);
        }

        public function delete() {
                $id = $this->_post('id', '');
                if (!$id) {
                        showError('参数错误');
                }
                M('guess_map')->delete(array('id' => $id));
                $this->JsonReturn('ok', null, 1);
        }

        /**
         * 验证题目是否雷同
         */
        public function checktitle() {
                $this->JsonReturn('ok', D('guess')->checkContent($this->_post('content'), $this->_post('id', '')), 1);
        }

        //状态修改
        public function deal() {
                $id = $this->_getid('id');
                $act = $this->_get('act');
                $key = $this->_get('key');
                if (!$id || !$key || !in_array($act, array('ok', 'invalid'))) {
                        showError('参数有误');
                }
                if ($key == 2) {
                        $verify = 2;
                        if ($act == 'ok') {
                                $verify = 1;
                        } else {
                                $verify = 2;
                        }
                        D('guess_map')->update(array('verify' => $verify), array('id' => $id));
                        $this->question($id);
                        $this->JsonReturn('ok', null, 1);
                } else {
                        $status = 2;
                        if ($act == 'ok') {
                                $status = 1;
                        } else {
                                $status = 2;
                        }
                        D('guess_map')->update(array('status' => $status), array('id' => $id));
                        $this->JsonReturn('ok', null, 1);
                }
        }

        /**
         * 统计
         */
        public function countinfo() {
                $num = 20; //排行榜显示数据
                $stime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
                $etime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
                $this->assign(array(
                  'peopleDay' => $this->getPeople(1), //当日参与人数
                  'peopleWeek' => $this->getPeople(7), //本周参与人数
                  'peopleMonth' => $this->getPeople(30), //本月参与人数
                  'peopleTotal' => $this->getPeople(0), //总参与人数
                  'titleDay' => $this->getTitle(1), //当日用户出题数
                  'titleTotal' => $this->getTitle(0), //总出题数
                  'integralRank' => D('guess')->getlist($stime, $etime, '', 5, 1, 0, $num), //当日积分排名前20
                  'rewardRank' => D('guess')->getReward($stime, $etime, $num), //当日连续答题前20
                  'wrongTitleRank' => D('guess')->getWrong($stime, $etime, $num), //当日错题排名前20
                  'topicRank' => D('guess')->getTopic($stime, $etime, $num), //当日出题用户前20
                  'topicTotal' => D('guess')->getTopic('', '', $num), //出题总用户前20
                  'num' => $num,
                ));
                //print_r($list);
                //  exit;
                //z($this->tVar);
                $this->display();
        }

        /**
         * 用户出题信息提示
         * @param type $id
         * @return type
         */
        private function question($id) {
                $guess = F('guess/guess');
                $rs = D('guess_map')->field('title,question,user_id')->where(array('id' => $id, 'source' => 2, 'verify' => 1))->find();
                if ($rs) {
                        if (!$rs['question']) {
                                //送出积分
                                $topic = isset($guess['topic']) ? $guess['topic'] : 0;
                                if ($topic) {
                                        D('member')->setUserScore(array('uid' => $rs['user_id'], 'act' => 1, 'event' => 5, 'fid' => 0, 'credit' => $topic, 'content' => '疯狂猜图：题目(' . $rs['title'] . ') 审核通过'));
                                        D('guess_map')->update(array('question' => 1), array('id' => $id));
                                        $this->send($rs['user_id'],$topic);
                                        //$this->JsonReturn('用户出题审核通过，积分赠送成功', null, 1);
                                } else {
                                        $this->JsonReturn('用户出题审核通过，没有设置赠送积分', null, 1);
                                }
                        } else {
                                $this->JsonReturn('用户积分已赠送过', null, 1);
                        }
                }
                return;
        }

        /**
         * 用户消息推送 
         * @param type $uid 用户id
         * @param type $topic 赠送积分
         * @return type
         */
        private function send($uid,$topic) {
                $touser = D('member')->getOpenidByUids($uid);
                if (!$touser) {
                        $this->JsonReturn('用户不存在', array('code' => '', 'msg' => ''), 0);
                }
                $sendInfo = array(
                  'touser' => $touser,
                  'msgtype' => 'text',
                  'text' => array(
                    'content' => '恭喜邻居的题目已通过审核，进入题库，送上'.$topic.'个积分，欢迎邻居再来出题哦！',//用户推送消息文本
                  )
                );
                T('weixin/weixin.api');
                $weixinMsgApi = new weixinMsgApi();
                if ($weixinMsgApi->sendCustomMessage($sendInfo)) {
                        return $this->JsonReturn('用户出题审核通过，积分赠送成功，消息回复成功', null, 1);
                } else {
                        return $this->JsonReturn('用户出题审核通过，积分赠送成功，消息回复失败', array('code' => $weixinMsgApi->errCode, 'msg' => $weixinMsgApi->errMsg), 0);
                }
        }

        /**
         * 栏目设置信息获取
         * @param type $boo
         * @return type
         */
        private function setobjData($boo = true) {
                $objData = array('aid' => admin::$adminInfo['aid'], 'title' => '疯狂猜图', 'type' => 1, 'everyday' => 0, 'shares' => 0, 'hasbind' => 0, 'reward_t' => 0, 'reward_i' => 0, 'topic' => 0,
                  'keyword' => '', 'recontent' => '', 'share_title' => '', 'share_content' => '', 'content' => '', 'skin_path' => '');
                //用户ID、标题、活动模板、每日允许答题数，0为不限制、分享到朋友圈增加题目数、用户入住额外增加题目数、连续答对多少答题、奖励多少积分、用户出题所赠积分
                //关键词、重复参加时的回复提醒、分享标题、分享内容、活动说明、应用模板路径
                if ($boo) {
                        foreach ($objData as $k => $v) {
                                $objData[$k] = $this->_post($k, $v);
                        }
                }
                F('guess/guess', $objData);
                return $objData;
        }

        //获取全部的插件模板
        private function getPluginTpl($type = 1) {
                $list = glob(ROOT . 'plugins/prize/*/config.php', GLOB_NOSORT);
                $arr = array('默认模板' => array('path' => 'default'));
                foreach ($list as $val) {
                        $data = @include $val;
                        if (isHave($data['aid']) && $data['aid'] == admin::$adminInfo['aid'] && isHave($data['type']) && $data['type'] == $type && checkPath($data['path'])) {
                                $arr[$data['name']] = $data;
                        }
                }
                return $arr;
        }

        /**
         * 获取活动参与人数
         * @param type $day 参与人数 1：当日，7,：近七日，30：近30日，0：总
         */
        private function getPeople($day) {
                $stime = mktime(0, 0, 0, date('m'), date('d') - ($day - 1), date('Y'));
                $etime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
                if (!$day) {
                        $stime = '';
                        $etime = '';
                }
                return D('guess')->getPeople($stime, $etime);
        }

        /**
         * 获取用户出题数
         * @param type $day 1：当日，0：总
         */
        private function getTitle($day) {
                $stime = mktime(0, 0, 0, date('m'), date('d') - ($day - 1), date('Y'));
                $etime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
                if (!$day) {
                        $stime = '';
                        $etime = '';
                }
                return D('guess')->getTitle($stime, $etime);
        }

}
